package com.jinwei.qunguanjia.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Utils {
    /**
     * 将InputStream转为Base64
     *
     * @param is 输入流
     * @return base64编码串
     * @throws Exception 异常
     */
    public static String inputStreamToBase64(InputStream is) throws Exception {
        byte[] data;
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc;
        while ((rc = is.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        data = swapStream.toByteArray();
        return Base64.getEncoder().encodeToString(data);
    }
    public static String decode(String base64Str) {
        Base64.Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(base64Str), StandardCharsets.UTF_8);
    }
}
